package com.atguigu.gulimall.order.web;

import com.atguigu.gulimall.order.exception.NoStockException;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.to.OrderCreateTo;
import com.atguigu.gulimall.order.vo.OrderConfirmVo;
import com.atguigu.gulimall.order.vo.OrderSubmitVo;
import com.atguigu.gulimall.order.vo.SubmitOrderResponseVo;
import org.apache.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

/**
 * @Classname OrderWebController
 * @Description TODO
 * @Date 2021/10/13 17:54
 * @Author Anonymous
 */
@Controller
public class OrderWebController {

    @Resource
    private OrderService orderService;

    /**
     * 去结算页确认
     */
    @RequestMapping("/toTrade")
    public String toTrade(Model model, HttpRequest request) throws ExecutionException, InterruptedException {
        OrderConfirmVo orderConfirmVo = orderService.confirmOrder();
        //展示订单确认的数据
        model.addAttribute("orderConfirmData", orderConfirmVo);
        return "confirm";
    }

    /**
     * 提交订单下单
     */
    @RequestMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo submitVo, Model model, RedirectAttributes attributes) {
        //库存锁定失败会抛出异常
        try {
            SubmitOrderResponseVo submitOrderResponseVo = orderService.submitOrder(submitVo);
            //一旦这个方法执行，就应该去服务器创建订单，验证令牌，验证价格等，锁库存
            //下单成功来到支付选择页
            //下单失败回到订单确认页重新确认订单信息
            if (submitOrderResponseVo.getCode() == 0) {
                model.addAttribute("submitOrderResponseVo", submitOrderResponseVo);
                //下单成功到支付页面
                return "pay";
            } else {
                String msg = "下单失败";
                switch (submitOrderResponseVo.getCode()) {
                    case 1:
                        msg += "订单信息过期，请重新提交";
                        break;
                    case 2:
                        msg += "订单商品价格发送变化,请确认后再次提交";
                        break;
                    case 3:
                        msg += "商品库存不足";
                        break;
                    default:
                        msg += "未知错误";
                }
                //重定向携带数据，实际是放入session，前端通过session.msg取值即可
                attributes.addFlashAttribute("msg", msg);
                //下单失败重新返回订单确认页
                return "redirect:http://order.gulimall.com/toTrade";
            }
        } catch (Exception e) {
            //instanceof的作用：它的作用是测试它左边的对象是否是它右边的类的实例，返回 boolean 的数据类型。
            // https://www.runoob.com/java/method-instanceof.html
            //一般向下转型时候使用
            if (e instanceof NoStockException) {
                String msg = ((NoStockException) e).getMessage();
                attributes.addFlashAttribute("msg", msg);
            }
            return "redirect:http://order.gulimall.com/toTrade";
        }
    }
}
